package cn.hl.hm.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hl.hm.entity.Rebate;
import cn.hl.hm.service.RebateService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
@Controller
@RequestMapping("/rebateController")
public class RebateController {
	
	private static final Logger logger = LoggerFactory.getLogger(RebateController.class);
	SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	RebateService rebateService;
	 
	 
	//添加折扣
	@RequestMapping(value = "/addRebate", method = RequestMethod.POST)
	@ResponseBody
	public String addRebate(HttpServletRequest request, Rebate rebate ,
			
			HttpServletResponse response) {
		logger.info("添加折扣");
		
		rebate.setCreateTime(new Date());
		rebate.setIsDelete(0);
		int add=rebateService.insertRebate(rebate);
		
		if (add > 0) {
			Result.ok(null, InfoCode.SUCCESS, response);
			return null;
		} else {
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
	}
	
	
	//修改折扣
	@RequestMapping(value = "/updateRebate", method = RequestMethod.POST)
	@ResponseBody
	public String updateRebate(HttpServletRequest request, Rebate rebate ,
			
			HttpServletResponse response) {
		logger.info("修改折扣");
		
		//修改(软删除)
		
		
		//根据id出这条数据
		Rebate sj=rebateService.selectRebate(rebate.getId());
		
		Rebate updateRebate= new Rebate();
		updateRebate.setId(rebate.getId());
		updateRebate.setIsDelete(1);
		updateRebate.setEndTime(new Date());
		updateRebate.setCreateTime(sj.getCreateTime());
		updateRebate.setTop(sj.getTop());
		updateRebate.setFloor(sj.getFloor());
		updateRebate.setRebate(sj.getRebate());
		
		int update = rebateService.updateRebate(updateRebate);
		

	    if(update>0){
	    	//新增
	    	Rebate addRebate= new Rebate();
	    	
	    	addRebate.setCreateTime(new Date());
	    	addRebate.setIsDelete(0);
	    	addRebate.setTop(rebate.getTop());
	    	addRebate.setFloor(rebate.getFloor());
	    	addRebate.setIsDelete(0);
	    	addRebate.setRebate(rebate.getRebate());
			int add=rebateService.insertRebate(addRebate);
			
			
			if(add>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
				   return null;
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		   
	    }else{
			 
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
		}
		
		
	}
		
	
	//删除折扣
	@RequestMapping(value = "/deleteRebate", method = RequestMethod.POST)
	@ResponseBody
	public String deleteRebate(HttpServletRequest request, Rebate rebate ,
			
			HttpServletResponse response) {
		logger.info("删除折扣");
		
		//软删除
		
		//根据id出这条数据
		Rebate sj=rebateService.selectRebate(rebate.getId());
		
		Rebate updateRebate= new Rebate();
		updateRebate.setId(rebate.getId());
		updateRebate.setIsDelete(1);
		updateRebate.setEndTime(new Date());
		updateRebate.setCreateTime(sj.getCreateTime());
		updateRebate.setTop(sj.getTop());
		updateRebate.setFloor(sj.getFloor());
		updateRebate.setRebate(sj.getRebate());
		
		int update = rebateService.updateRebate(updateRebate);

	    if(update>0){
	    	
			Result.ok(null, InfoCode.SUCCESS, response);
			return null;
		   
	    }else{
			 
			Result.ok(null, InfoCode.FAILURE, response);
			return null;
	    }
			
			
	}
	
	
	//折扣列表
	@RequestMapping(value = "/selectAllInfo", method = RequestMethod.POST)
	public String selectAllInfo(HttpServletRequest request, 
			
			HttpServletResponse response) {
		logger.info("折扣列表");
		
		List<Rebate> selectInfo=rebateService.selectAllInfo();

	    if(selectInfo!=null&&selectInfo.size()>0){
	    	
			Result.ok(selectInfo, InfoCode.SUCCESS, response);
			return null;
		   
	    }else{
			 
			Result.ok(null, InfoCode.SUCCESS, response);
			return null;
		}
			
	}
	
	
	//折扣详情
	@RequestMapping(value = "/rebateInfo", method = RequestMethod.POST)
	public String rebateInfo(HttpServletRequest request, Rebate rebate ,
			
			HttpServletResponse response) {
		
		logger.info("折扣详情");
		
		
		//根据id查出这条数据
		Rebate rebateInfo=rebateService.selectRebate(rebate.getId());
		

	    if(rebateInfo!=null){
	    	
				Result.ok(rebateInfo, InfoCode.SUCCESS, response);
				return null;
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
				return null;
			}
		   
	    }

	

}
